package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Business;
import model.BusinessTypeEnum;
import model.Student;
import model.Teacher;
import persistency.ArgIO;

public class TeacherController extends BusinessController{
	private StringBuilder callableStatement;

	
	private static TeacherController instance = new TeacherController();
	private TeacherController(){}
	
	public static TeacherController instance(){
		return instance;
	}
	
	public boolean insertTeacher(Teacher teacher) {
		callableStatement = new StringBuilder("{call sp_insertteacher(?,?,?)}");
		return insert(teacher,callableStatement,3);
	}
	
	public Teacher getTeacher(String id){
		return (Teacher)getBusinessObject(id,new StringBuilder("{call sp_getbyidteacher(?)}"),BusinessTypeEnum.TEACHER);
	}
	
	public Collection<Teacher> getAll() {
		Collection<Teacher> col = new ArrayList<Teacher>();
		col.addAll((Collection<? extends Teacher>) getBusinessObjects(new StringBuilder("{call sp_getallteacher}"),BusinessTypeEnum.TEACHER));
		return col;
	}

	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		/*
		 in teachercode varchar(25),
   	 in name varchar(25),
   	 in amountwantedhours integer
		 */
		
		Teacher teacher = (Teacher) business;
		switch (i) {
		case 0: {
			args[i] = teacher.getTeacherCode();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = teacher.getTeacherName();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 2: {
			args[i] = teacher.getAmountWantedHours();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}
}
